<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../../css/element.css">
    <title>双色球生成器-Vue3版</title>
    <style>
        * {
            padding: 0px;
            margin: 0px;
            box-sizing: border-box;
            list-style: none;
        }

        #app {
            width: 1200px;
            margin: auto;
        }

        .el-form {
            margin-top: 20px;
            padding: 5px;
            border: 1px solid #dcdfe6;
            border-radius: 5px;
        }

        .el-descriptions {
            margin-top: 20px;
            padding: 5px;
            border: 1px solid #dcdfe6;
            border-radius: 5px;
        }

        .ball {
            width: 40px;
            height: 40px;
            text-align: center;
            border-radius: 50%;
            line-height: 40px;
            font-weight: 600;
            font-size: 110%;
            color: #fff;
            display: inline-block;
        }

        .red-ball {
            background-color: #f30000;
            margin-right: 10px;
        }

        .blue-ball {
            background-color: #2663fb;

        }
    </style>
</head>

<body>
    <main id="app">
        <el-form :model="formData" label-width="120px" :rules="rules" ref="myForm">
            <el-form-item label="随机注数" prop="count">
                <el-input-number v-model="formData.count" :min="1" :max="99" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(myForm)">确认</el-button>
            </el-form-item>
        </el-form>
        <el-descriptions title="结果" :column="1">
            <el-descriptions-item v-for="item in listData" :key="item">
                <span v-for="(item2,idx) in item" :key="item2"
                    :class="idx <= 5 ? 'red-ball ball' : 'blue-ball ball'">{{item2}}</span>
            </el-descriptions-item>
        </el-descriptions>
        </template>
    </main>
</body>
<script src="../../js/vue.global.js"></script>
<script src="../../js/element.full.js"></script>
<script>
    // 获取随机数：[0,max)
    function getRandom(max) {
        return Math.floor(Math.random() * max);
    }

    const red = [];

    const blue = [];

    /**
     * 初始化
     */
    function initBall() {
        // 红球：1-33 篮球：1-16
        for (let i = 1; i <= 33; i++) {
            red.push(i);
            if (i <= 16) {
                blue.push(i);
            }
        }
    }

    /*
     * 随机一注双色球
     */
    function getOne() {
        // 将红色球的池子，拷贝到新数组中
        let red2 = red.map(v => v);

        let result = [];

        // 随机六个红色球
        for (let i = 0; i < 6; i++) {
            let index = getRandom(red2.length);
            let value = red2.splice(index, 1);
            result.push(value[0]);
        }

        // 排序
        result.sort((n, m) => n - m);

        // 添加蓝球
        let index = getRandom(blue.length);
        result.push(blue[index]);

        return result;

    }


    const { createApp, ref, reactive } = Vue;
    const App = {
        setup() {
            initBall();

            const formData = reactive({
                count: 5
            });

            const listData = reactive([]);

            const rules = reactive({
                count: [
                    { required: true, message: '请选择随机的注数', trigger: 'blur' },
                ]
            });

            const myForm = ref();

            const submitForm = (form) => {
                if (!form) {
                    return
                }
                console.log("form = ", form);
                form.validate(valid => {
                    if (!valid) {
                        return;
                    }
                    for (let i = 0; i < formData.count; i++) {
                        listData.push(getOne());
                    }
                })
            }

            return {
                formData, rules, myForm, submitForm, listData
            };
        },
    };

    const app = createApp(App);


    app.use(ElementPlus);
    app.mount("#app");
</script>

</html>